<?php 
class contentobject{
	
	public function getContentAndElem($param=array()){
		$classDatabase = Import::loadClass('database', 'library/');
		$data = $this->getContent($param);
		
		if(is_array($data)){
			foreach($data as $key => $val){
				$data[$key] = $val;
				$data[$key]['elems'] = $this->getContentElem($val['contentobject_id']);
			}
		}
//			echo'<pre>';
//		print_r($data);
//		echo'</pre>';
		return $data;
		
	}
	
	public function getContentAndElemByCollection($param=array()){
		$data = $this->getContentAndElem($param);
		
		
		foreach($data as $key => $val){
			if( $val['collection_id'] <> $param['contentobject_structure_id'])
			unset($data[$key]);
		}
		return $data;
	}
	
	public function getContent($param=array()){
		$classDatabase = Import::loadClass('database', 'library/');
		
		$where='';
		if(isset($param['contentobject_id'])){
			$where = ' AND contentobject_id = "'.$param['contentobject_id'].'"';
		}
		
		
		$classDatabase = Import::loadClass('database', 'library/');
		$q = 'SELECT contentobject_id, collection_id, contentobject_structure_id, contentobject_publish, contentobject_position, contentobject_date_create, contentobject_date_update, contentobject_date_delete 
		 FROM contentobject WHERE contentobject_date_delete=0'.$where;	
		$data = $classDatabase->exec($q);
		return $data;
	}
	
	public function getContentElem($param=array()){
		$classDatabase = Import::loadClass('database', 'library/');
		
		$where='';
		if(isset($param['contentobject_id'])){
			$where = ' WHERE contentobject_id = "'.$param['contentobject_id'].'"';
		}
		
		
		$classDatabase = Import::loadClass('database', 'library/');
		$q = 'SELECT contentobject_value_id, contentobject_value_text, contentobject_id 
		 FROM contentobject_value '.$where .' ORDER BY contentobject_value_id';	
		$data = $classDatabase->exec($q);
		return $data;
	}
	
	public function createContent($param){
		$classDatabase = Import::loadClass('database', 'library/');
		
//		echo'<pre>';
//			print_r($param);
//		echo'</pre>';
//		die;
		
		$q = sprintf('INSERT INTO contentobject (collection_id, contentobject_structure_id, contentobject_publish, contentobject_position, contentobject_date_create) VALUES
					("%s", "%s", "%s", "%s", NOW())', $param['contentobject']['collection_id'],$param['contentobject']['contentobject_structure_id'], $param['contentobject']['contentobject_publish'], 0);	
		$contentobject_id = $classDatabase->exec($q);
		foreach($param['contentobject']['contentobject_value'] as $key => $val){
			$q = sprintf('INSERT INTO contentobject_value (contentobject_id, contentobject_value_text) VALUES
					("%s", "%s")', $contentobject_id, $val);	
			$classDatabase->exec($q);
		}

	}
	
	public function createStructure($param){
		$classDatabase = Import::loadClass('database', 'library/');
		$q = sprintf('INSERT INTO contentobject_structure (contentobject_structure_label, contentobject_structure_caption, contentobject_structure_date_create) VALUES
					("%s", "%s", NOW())', $param['contentobject_structure']['contentobject_structure_label'], $param['contentobject_structure']['contentobject_structure_caption']);	
			
		$contentobject_structure_id = $classDatabase->exec($q);

		foreach($param['contentobject_structure']['elems'] as $key => $val){
			$q = sprintf('INSERT INTO contentobject_structure_elem (contentobject_structure_id, input_fields_id, contentobject_structure_elem_label, contentobject_structure_elem_caption, contentobject_structure_elem_position) VALUES
					("%s", "%s", "%s", "%s", "%s")', $contentobject_structure_id, $val['contentobject_structure_type'], $val['contentobject_structure_label'], $val['contentobject_structure_caption'], $val['contentobject_structure_position']);	
			$classDatabase->exec($q);
		}
		
		return $contentobject_structure_id;
		
	}
	
	public function editStructure($param){
		$classDatabase = Import::loadClass('database', 'library/');
		
//		echo'<pre>';
//			print_r($param);
//		echo'</pre>';
		
		$q = sprintf('UPDATE contentobject_structure set contentobject_structure_label="%s", contentobject_structure_caption="%s", contentobject_structure_date_update=NOW()) WHERE contentobject_structure_id ="'.$param['contentobject_structure']['contentobject_id'].'"',  $param['contentobject_structure']['contentobject_structure_label'], $param['contentobject_structure']['contentobject_structure_caption']);	
		$classDatabase->exec($q);
		
		$q = 'DELETE FROM contentobject_structure_elem WHERE contentobject_structure_id = "'.$param['contentobject_structure']['contentobject_id'].'"';	
		$classDatabase->exec($q);
		
		foreach($param['contentobject_structure']['elems'] as $key => $val){
			$q = sprintf('INSERT INTO contentobject_structure_elem (contentobject_structure_id, input_fields_id, contentobject_structure_elem_label, contentobject_structure_elem_caption, contentobject_structure_elem_position) VALUES
					("%s", "%s", "%s", "%s", "%s")', $param['contentobject_structure']['contentobject_id'], $val['contentobject_structure_type'], $val['contentobject_structure_label'], $val['contentobject_structure_caption'], $val['contentobject_structure_position']);	
			$classDatabase->exec($q);
		}
		
	}
	
	public function getList($param=array()){
		$where='';
		if(isset($param['utilisateur_date_delete'])){
			$where = ' WHERE utilisateur_date_delete <>0';	
		}else{
			$where = ' WHERE utilisateur_date_delete =0';	
		}
		
		$classDatabase = Import::loadClass('database', 'library/');
		$q = sprintf('SELECT utilisateur_id, groupe_id, utilisateur_email, utilisateur_firstname, utilisateur_lastname, utilisateur_password, utilisateur_date_create, utilisateur_date_update, utilisateur_date_delete FROM utilisateur '.$where);	
		$data = $classDatabase->exec($q);
		
		
//		echo'<pre>';
//		print_r($data);
//		echo'</pre>';
		return $data;
		
	}	
	
	
	public function getStructureAndStructureElem($param=array()){
		$data = $this->getStructure($param);
		
		foreach($data as $key => $val){
			$data[$key] = $val;
			$data[$key]['elems'] = $this->getStructureElem($val['contentobject_structure_id']);
		}
//			echo'<pre>';
//		print_r($data);
//		echo'</pre>';
		return $data;
	}
	
	public function getStructure($param=array()){
		$where='';
		if(isset($param['contentobject_structure_id'])){
			$where = ' AND contentobject_structure_id = "'.$param['contentobject_structure_id'].'"';
		}
		
		
		$classDatabase = Import::loadClass('database', 'library/');
		$q = 'SELECT contentobject_structure_id, contentobject_structure_label, contentobject_structure_caption,
contentobject_structure_date_create, contentobject_structure_date_update, contentobject_structure_date_delete
		 FROM contentobject_structure WHERE contentobject_structure_date_delete=0'.$where;	
		$data = $classDatabase->exec($q);
		return $data;
	}
	
	public function getStructureElem($data=array()){
		$where='';
		if(isset($data['contentobject_structure_id'])){
			$where = ' WHERE contentobject_structure_id = "'.$data['contentobject_structure_id'].'"';
		}
		
		$classDatabase = Import::loadClass('database', 'library/');
		$q = 'SELECT contentobject_structure_elem_id,contentobject_structure_id, input_fields_id, 
contentobject_structure_elem_label, contentobject_structure_elem_caption, contentobject_structure_elem_position
		 FROM contentobject_structure_elem '.$where.' ORDER BY contentobject_structure_elem_position asc';
		
		$data = $classDatabase->exec($q);
		
		return $data;
		
	}
	
	function getSingleton (){
		return true;
	}
}
?>